﻿@inject IDataCacheService _dataCacheService

@inject IPopupService PopupService

<div class="@Class">
    <div class="pb-2" style="display: flex; justify-content: center; text-align: center;">
        目前只支持知乎和B站链接，格式如下<br />
        https://zhuanlan.zhihu.com/p/480692805 , https://www.bilibili.com/read/cv19750701

    </div>    <MRow Class="mb-4">
        <MCol Cols="12" Md="3">
            <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="InputModel.Type" TValue="ArticleType" Label="类型" />

        </MCol>
        <MCol Cols="12" Md="9">
            <MTextField Label="标题（可不填，将自动获取）" @bind-Value="@InputModel.Title" TValue="string"></MTextField>

        </MCol>
        <MCol Cols="12">
            <MTextField Label="链接" @bind-Value="@InputModel.Url" TValue="string"></MTextField>

        </MCol>
        <MCol Cols="12">
            <MCheckbox @bind-Value="InputModel.IsCreatedByCurrentUser" Label="本人创作" />

        </MCol>
        <MCol Cols="12">
            <CnGalWebSite.Components.Buttons.MasaButton Rounded Text="添加" OnClick="@(async ()=> {await OnAdd(InputModel);InputModel = new RepostArticleModel(); })" Icon="mdi-plus" />
         </MCol>
     </MRow>

    <MSimpleTable Class="mb-4">
        <thead>
            <tr>
                <th class="text-left">
                    类型
                </th>
                <th class="text-left">
                    标题
                </th>
                <th class="text-left">
                    链接
                </th>
                <th class="text-left">
                    操作
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr @key="item.Url">
                    <td>@item.Type.GetDisplayName()</td>
                    <td>@item.Title</td>
                    <td>@item.Url</td>
                    <td>
                        <CnGalWebSite.Components.Buttons.MasaButton Text="删除" TextStyle OnClick="@(()=>OnDelete(item))" Icon="mdi-trash-can" Color="error" />
                    </td>
                </tr>
            }
        </tbody>
    </MSimpleTable>

    <CnGalWebSite.Components.Buttons.MasaButton Text="下一步" Rounded Icon="mdi-arrow-down-bold-outline" OnClick="OnClickFinshInput" Class="mb-2" />
</div>
<ImportCard @ref="importCard" OnImported="OnImported" />


@code {
    [Parameter]
    public List<RepostArticleModel> Model { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]

    public EventCallback OnFinshInput { get; set; }
    List<KeyValuePair<string, ArticleType>> items = new List<KeyValuePair<string, ArticleType>>();

    RepostArticleModel InputModel = new RepostArticleModel();

    ImportCard importCard;


    protected override async Task OnInitializedAsync()
    {
        items.Clear();
        for (var i = 0; i < 9; i++)
        {
            items.Add(new KeyValuePair<string, ArticleType>(((ArticleType)i).GetDisplayName(), (ArticleType)i));
        }
    }

    public void ValueChanged(ArticleType type)
    {
        InputModel.Type = type;
        
    }

    public async Task OnImported(List<string> items)
    {
        foreach (var item in items)
        {
            var model = new RepostArticleModel
            {
                Url = item,
                Type=InputModel.Type,
                IsCreatedByCurrentUser = InputModel.IsCreatedByCurrentUser
            };
            await OnAdd(model);
        }
    }
    public async Task OnImport()
    {
        await importCard.Refresh(true);
    }

    public async Task OnAdd(RepostArticleModel model)
    {
        model.UserName = _dataCacheService.UserInfor.Name;

        if (string.IsNullOrWhiteSpace(model.Url))
        {
            await PopupService.ToastAsync("先填写文章链接哦", AlertTypes.Error);
            return;
        }
        if (!model.Url.Contains("zhuanlan.zhihu.com") && !model.Url.Contains("www.bilibili.com"))
        {
            await PopupService.ToastAsync("链接格式不正确", AlertTypes.Error);
            return;
        }
        if (Model.Any(s => s.Url == model.Url))
        {
            await PopupService.ToastAsync("文章链接添加一次就够了哦", AlertTypes.Error);
            return;
        }

        Model.Add(model);
    }

    public void OnDelete(RepostArticleModel model)
    {
        Model.Remove(model);
        
    }

    public async Task OnClickFinshInput()
    {
        if (Model.Count == 0)
        {
            await PopupService.ToastAsync("你还没有添加需要转载的文章链接哦", AlertTypes.Error);
            return;
        }
        await OnFinshInput.InvokeAsync();
    }
}
